const chalk = require('chalk');

const download =  require("download-git-repo");
const ora = require("ora");
const inquirer = require("inquirer");

const remoteList = {
    "kotlin": "https://gitee.com/haizhilingyu/template.git",
    "java": "https://gitee.com/haizhilingyu/template-java.git",
    "scui": "https://gitee.com/lolicode/scui.git",
  };

const create = (remote, name, option) => {
    const downSpinner = ora("正在下载模板...").start();
    return new Promise((resolve, reject) => {
        download(remote, name, option, (err) => {
        if (err) {
            downSpinner.fail();
            console.log("err", chalk.red(err));
            reject(err);
            return;
        }
        downSpinner.succeed(chalk.green("模板下载成功！"));
        resolve();
        });
    });
};

module.exports = ()=>{
inquirer
    .prompt([
    {
        type: "input",
        name: "projectName",
        message: "请输入项目名称",
    },
    {
        type: "list",
        name: "language",
        message: "请选择开发语言",
        choices: [
        {
            name: "Kotlin",
            value: 'kotlin',
            description: "Kotlin版本Jimmer",
        },
        {
            name: "Java",
            value: 'java',
            description: "Java版本Jimmer",
        },
        ],
    },
    {
        type: "list",
        name: "frontend",
        message: "请选择前端框架",
        choices: [
        {
            name: "Vue3",
            value: 'scui',
            description: "SCUI Vue3+Element Plus封装的框架",
        },
        ],
    },
    ])
    .then((answers) => {
        create(`direct:${remoteList[answers.language]}`, answers.projectName+'/backend', { clone: true });
        create(`direct:${remoteList[answers.frontend]}`, answers.projectName+'/frontend', { clone: true });
    });
}